package com.ruoyi.web.controller.biz;

import com.ruoyi.biz.domain.BizParcel;
import com.ruoyi.biz.domain.query.BizParcelQuery;
import com.ruoyi.biz.domain.vo.BizParcelVO;
import com.ruoyi.biz.service.IBizParcelService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.MultipartRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 邮包Controller
 *
 * @author ruoyi
 * @date 2022-01-10
 */
@RestController
@RequestMapping("/biz/parcel")
public class BizParcelController extends BaseController {
    @Autowired
    private IBizParcelService bizParcelService;

    /**
     * 查询邮包列表
     */
    @GetMapping("/list")
    public TableDataInfo list(BizParcelQuery bizParcelQuery) {
        startPage();
        List<BizParcel> list = bizParcelService.selectBizParcelList(bizParcelQuery);
        return getDataTable(list);
    }

    /**
     * 导出邮包列表
     */
    @Log(title = "邮包", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizParcelQuery bizParcelQuery) {
        List<BizParcel> list = bizParcelService.selectBizParcelList(bizParcelQuery);
        ExcelUtil<BizParcel> util = new ExcelUtil<BizParcel>(BizParcel.class);
        util.exportExcel(response, list, "邮包数据");
    }

    /**
     * 获取邮包详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:parcel:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(bizParcelService.selectBizParcelById(id));
    }

    /**
     * 新增邮包
     */
    @Log(title = "邮包新增", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(BizParcel bizParcel, HttpServletRequest request) {
        List<MultipartFile> files = ((MultipartHttpServletRequest)request).getFiles("img");
        return toAjax(bizParcelService.insertParcel(bizParcel, files));
    }

    /**
     * 修改邮包
     */
    @Log(title = "邮包", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(BizParcel bizParcel) {
        return toAjax(bizParcelService.updateBizParcel(bizParcel));
    }

    /**
     * 删除邮包
     */
    @PreAuthorize("@ss.hasPermi('system:parcel:remove')")
    @Log(title = "邮包", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(bizParcelService.deleteBizParcelByIds(ids));
    }


    @GetMapping("/select")
    public TableDataInfo select(BizParcelQuery bizParcelQuery) {
        startPage();
        List<BizParcelVO> list = bizParcelService.selectBizParcelVOList(bizParcelQuery);
        return getDataTable(list);
    }
}
